//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.Expressions
{
    class FloatingColumnReference: ColumnReference
    {
        public FloatingColumnReference(Worksheet worksheet, int offset, bool isLocked)
        {
            _worksheet = worksheet;
            _offset = offset;
            this._isLocked = isLocked;
        }

        internal override DimensionReference<Row, Column> SwapDimension()
        {
            return new FloatingRowReference(Worksheet, _offset, IsLocked);
        }


        readonly int _offset;
        readonly Worksheet _worksheet;
        private bool _isLocked;

        internal override string GetText(TextContext context)
        {
            Cell c = context.EvaluationContext.Target;
            int column = c.Column.Index + context.EvaluationContext.Offset.Column + _offset;
            return c.Worksheet.Columns[column].Label;
        }

        public override bool IsLocked
        {
            get { return _isLocked; }
        }

        internal override ColumnReference Projected(Worksheet targetWorksheet)
        {
            return new FloatingColumnReference(targetWorksheet, this._offset, this.IsLocked);
        }

        internal override Column GetColumn(EvaluationContext context)
        {
            return _worksheet.Columns[context.Target.Column.Index + _offset];
        }
        internal override ColumnReference Offset(CellRelativeAddress address)
        {
            if (IsLocked)
            return this;
            return new FloatingColumnReference(Worksheet, this._offset + address.Column, IsLocked);
        }

        public override Worksheet Worksheet
        {
            get { return _worksheet; }
        }

        public override string Label
        {
            get { return "C["+_offset+"]"; }
        }

        public override Column Anchor
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        internal int ColumnOffset
        {
            get { return _offset; }
        }
    }
}
